package com.magicodex.bookkeeping.ui.accounting;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.magicodex.bookkeeping.biz.accounting.constant.AccountingConstants;
import com.magicodex.bookkeeping.biz.accounting.dto.AccountingDTO;
import com.magicodex.bookkeeping.biz.accounting.event.AccountingItemClickListener;
import com.magicodex.bookkeeping.biz.accounting.event.AddFabClickListener;
import com.magicodex.bookkeeping.biz.accounting.event.DeleteFabClickListener;
import com.magicodex.bookkeeping.biz.accounting.event.EditFabClickListener;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingDataUtil;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingJsonUtil;
import com.magicodex.bookkeeping.biz.common.util.FileUtil;
import com.magicodex.bookkeeping.biz.common.util.StringUtil;
import com.magicodex.bookkeeping.databinding.FragmentAccountingBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AccountingFragment extends Fragment {

    private FragmentAccountingBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        AccountingViewModel accountingViewModel =
                new ViewModelProvider(this).get(AccountingViewModel.class);

        binding = FragmentAccountingBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        Activity activity = getActivity();
        ListView listView = binding.accountingList;
        FloatingActionButton addButton = binding.fabAdd;
        FloatingActionButton editButton = binding.fabEdit;
        FloatingActionButton deleteButton = binding.fabDelete;

        // 账户列表
        List<AccountingDTO> recordList = new ArrayList<>(loadData());
        AccountingDataUtil.sortAccountingRecords(recordList);
        AccountingListViewAdapter adapter = new AccountingListViewAdapter(activity, recordList);
        listView.setAdapter(adapter);

        // 监听事件
        listView.setOnItemClickListener(new AccountingItemClickListener(activity, adapter));
        addButton.setOnClickListener(new AddFabClickListener(activity, adapter));
        editButton.setOnClickListener(new EditFabClickListener(activity, adapter));
        deleteButton.setOnClickListener(new DeleteFabClickListener(activity, adapter));

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    /**
     * 加载数据
     *
     * @return
     */
    protected List<AccountingDTO> loadData() {
        Context context = getContext();
        String json = FileUtil.readFile(context, AccountingConstants.DATA_PATH);

        if (StringUtil.isEmpty(json)) {
            return Collections.emptyList();
        }

        List<AccountingDTO> recordList = AccountingJsonUtil.parseAccountingDTOs(json);

        return recordList;
    }

}